﻿--[[
   @brief    sys setting dialog contorl
   @author   hqzheng
   @version  1.0
   @date     2017-05-024
--]]

setting_qos_name = {
    kSettingName = "cancel_subscription_auto_downlaod";
};

setting_qos_action = {
    kActionChecked = "check";
    kActionUncheck = "uncheck";
};

setting_dialog_control =
{
    init = function()
        base_utility.log("setting_dialog_control.init called");
        ierd_tgp.tpf_ui.Require("main_menue_sys_setting\\model.lua");
        local tpfui =
          base_utility.get_component_adapter("ITpf_ui",
              "ierd_tgp.tpf_ui.GetTPFUI();");
        if nil ~= tpfui then
            tpfui:AddLuaListener(ierd_tgp.game_library.K_GET_SETTING_CONFIG, "setting_dialog_control.get_setting_config_rsp");
            tpfui:AddLuaListener(ierd_tgp.game_library.ANCHOR_SUBSCRIBE_FLAG_UPDATED, "setting_dialog_control.on_anchor_subscribe_flag_updated");
        end
        comm_center.listen_broadcast("Msg_MainWndShow", "setting_dialog_control.on_main_wnd_show");
        comm_center.listen_broadcast("Msg_OperateGameDurationRsp", "setting_dialog_control.on_operation_duration_rsp");
    end;

    inner_log = function (msg, core_msg)
        base_utility.log(string.format("[setting_dialog_view] %s", msg), core_msg);
    end;
    
    open_setting_dialog = function(event_id, json_str)
        setting_dialog_control.inner_log("setting_dialog_control recve event");
        ierd_tgp.tpf_ui.Require("main_menue_sys_setting\\view.lua");
        setting_dialog_control.get_vendor_info();
--        local uilib = ui_utility.uilib();
--        local src_table_param = base_utility.json_to_table(json_str);
--        local param_tab = {};
--        param_tab["tab_page"] = src_table_param.tab_id % 2;
--        param_tab["reason"] = 0;
--        -- param_tab["anchor_subscribe"] = 1;
--        -- param_tab["video_subscribe"] = 1;
--        local param_json = base_utility.table_to_json(param_tab);
--        setting_dialog_view.show(param_json);        
        
         local param = {
            url =  LoadStr("SYS_SETTING_PAGE_URL");
            caption =  LoadStr("ID_TPF_GENERAL_SETTINGS");
            use_ptlogin = false;
            width = 682;
            height = 536;
            show_loading_page = false;
            region_set_zero = true;
            reload = true;
            title_in_web = true;
        };
        setting_dialog_control.dialog_id = comm_center.call_service("i_service_show_simple_web_window", param);

    end;

    close_setting_dialog = function(data)
        local param = {id =  setting_dialog_control.dialog_id};
        comm_center.call_service("i_service_close_simple_web_window",param);
    end;

    open_setting_dialog_for_open_link = function(event_id, json_str)
        ierd_tgp.tpf_ui.Require("main_menue_sys_setting\\view.lua");
        local src_table_param = base_utility.json_to_table(json_str);
        if src_table_param["type"] == "setting" then
            local param_tab = {};
            param_tab["reason"] = 0;
            param_tab["tab_page"] = 0;
            if src_table_param["sub_page"] == "ROUTINE_SETTING" then
                param_tab["tab_page"] = 0;
            elseif src_table_param["sub_page"] == "GAME_DL_SETTING" then
                param_tab["tab_page"] = 1;
            end
            param_tab["anchor_subscribe"] = 1;
            param_tab["video_subscribe"] = 1;
            local param_json = base_utility.table_to_json(param_tab);
            setting_dialog_view.show(param_json);

            local gamelib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
            if gamelib ~= nil then
                gamelib:GetAnchorSubscribeFlag();
            end
            setting_dialog_control.get_video_subscribe_flag();
        end
    end;
    
    --获取成功返回选择的路径
    get_path_rsp = function(event_id, json_str)
        local tpfui =
      base_utility.get_component_adapter("ITpf_ui",
          "ierd_tgp.tpf_ui.GetTPFUI();");
          tpfui:RemoveLuaListener(ierd_tgp.game_library.DIR_SELECT_RESUTL, "setting_dialog_control.get_path_rsp");
          setting_dialog_view.get_path_rsp(json_str);
    end;

    get_path = function(data)
        local gamelib = base_utility.get_component_adapter("IGame_library",
               "ierd_tgp.game_library.Get_game_library_adapter();");
        if nil ~= gamelib then
            local dialog_ptr = ui_utility.get_ctrl_ptr_by_id(setting_dialog_control.dialog_id);

            local wnd = dialog_ptr:GetHwnd();
            local dir_initial_str = data.initial_path;
            local ret = gamelib:get_selected_dir_path(wnd, dir_initial_str);
            if ret == false then
--                tpfui:RemoveLuaListener(ierd_tgp.game_library.DIR_SELECT_RESUTL, "setting_dialog_control.get_path_rsp");
                base_utility.log("[setting_dialog_control]get_selected_dir_path return error!!!!", true);
            end
        end
    end;
    
    get_setting_config_req = function()
        setting_dialog_control.inner_log("get_setting_config_req");

        local data = comm_center.call_service("Svr_TGPConfigMgr_GetUserSysSetting", {});
        setting_dialog_view.set_setting_cfg(data);
    end;


    get_vendor_info = function()
        -- 海外版关闭路由设置
        if base_utility.is_type_of_oversea_ver() == true or setting_dialog_view.is_dev_client == true then
            return;
        end

        local device_mgr =
            base_utility.get_component_adapter("IDevice_mgr",
                "ierd_tgp.device.GetDeviceModule();");

        if device_mgr then
            local info = ierd_tgp.device.RouterDeviceStatus:new();
            device_mgr:GetRouterStatus(info);
            setting_dialog_model.vendor = info.vendor;
            setting_dialog_model.accl_status = info.accl_status;
            setting_dialog_model.is_predownload_status = info.predownload_status;
--            setting_dialog_model.vendor = "huawei";             --调试信息
--            setting_dialog_model.accl_status = true;            --调试信息
--            setting_dialog_model.is_predownload_status = true;  --调试信息
            base_utility.log("[Router_comm]The vendor of main_menue_setting is " .. setting_dialog_model.vendor);
            info:delete();
        end;
    end;
    -------------------------------------------------------------------
    -------------------------------------------------------------------
    -------------------------------------------------------------------
    --u8路径
    get_free_space = function(path, path_len)
        if nil == path then
            return;
        end

        base_utility.log("get_free_space, path:"..path..", len:"..path_len);
        local tfpui =
          base_utility.get_component_adapter("ITpf_ui",
              "ierd_tgp.tpf_ui.GetTPFUI();");
        if nil ~= tfpui then
            tfpui:AddLuaListener(ierd_tgp.game_library.K_GET_FREE_SPACE, "setting_dialog_control.get_free_space_rsp");
        end

        local gamelib =
          base_utility.get_component_adapter("IGame_library",
              "ierd_tgp.game_library.Get_game_library_adapter();");
        if nil ~= gamelib then
            gamelib:get_free_space(path, path_len);
        end
    end;
    
    get_free_space_rsp = function(event_id, json_str)
        base_utility.log("get_free_space_rsp");
        local tpfui =
          base_utility.get_component_adapter("ITpf_ui",
              "ierd_tgp.tpf_ui.GetTPFUI();");
        if nil ~= tpfui then
            tpfui:RemoveLuaListener(ierd_tgp.game_library.K_GET_FREE_SPACE, "setting_dialog_control.get_free_space_rsp");
        end
        setting_dialog_view.get_free_space_resp(json_str);
    end;
    
    set_setting_config = function(json_param)        
        local auto_start = json_param["auto_start"];
        base_utility.log("auto_start:"..auto_start);
        local auto_login = json_param["auto_login"];
        base_utility.log("auto_login:"..auto_login);
        local download_path = json_param["download_path"];
        base_utility.log("download_path:"..download_path);
        local speed_limit = json_param["download_speed_limit"];
        base_utility.log("speed_limit:"..speed_limit);
        local download_speed = json_param["download_speed"];
        base_utility.log("download_speed:"..download_speed);
        local setup_path = json_param["setup_path"];
        local closemainWndOper = json_param["close_main_wnd_oper"];
        base_utility.log("closemainWndOper:"..closemainWndOper);
        local iAutoDownloadUncompleteTask = json_param["auto_download_uncomplete_task"];
        base_utility.log("iAutoDownloadUncompleteTask:"..iAutoDownloadUncompleteTask);
        local iAfterInstallPkgOper = json_param["after_install_pkgoper"];
        base_utility.log("iAfterInstallPkgOper:"..iAfterInstallPkgOper);
        local iUseRouterDownloadTask = json_param["use_router_download_task"];
        base_utility.log("iUseRouterDownloadTask:"..iUseRouterDownloadTask);
        local iUseRouterAccelerate = json_param["use_router_accelerate"];
        base_utility.log("iUseRouterAccelerate:"..iUseRouterAccelerate);

        local start_game_tgp_wnd_oper = json_param["start_game_tgp_wnd_oper"];
        base_utility.log("start_game_tgp_wnd_oper:"..start_game_tgp_wnd_oper);

        local after_buy_game_oper = json_param["after_buy_game_oper"];
        base_utility.log("after_buy_game_oper:" .. after_buy_game_oper);

        local open_game_list_cloud_sync = json_param["open_game_list_cloud_sync"];
        base_utility.log("open_game_list_cloud_sync:" .. open_game_list_cloud_sync);

        local open_wegame_silent_update = json_param["open_wegame_silent_update"];
        base_utility.log("open_wegame_silent_update:" .. open_wegame_silent_update);
        
        local game_screenshot_hotkey = json_param["game_screenshot_hotkey"];
        base_utility.log("open_wegame_silent_update:" .. open_wegame_silent_update);

        local game_streaming_enable = json_param["game_streaming_enable"];
        base_utility.log("game_streaming_enable:" .. game_streaming_enable);
        
        local game_streaming_password = json_param["game_streaming_password"];
        base_utility.log("game_streaming_password:" .. game_streaming_password);
        
        local game_streaming_enable_password = json_param["game_streaming_enable_password"];
        base_utility.log("game_streaming_enable_password:" .. game_streaming_enable_password);
        
        local page_cache_setting = json_param["page_cache_setting"];
        base_utility.log("page_cache_setting:" .. page_cache_setting);

        local page_preload_setting = json_param["page_preload_setting"];
        base_utility.log("page_preload_setting:" .. page_preload_setting);

        local subscribe_after_open_download = json_param["subscribe_after_open_download"];
        base_utility.log("subscribe_after_open_download:" .. subscribe_after_open_download);

        -- 通知关闭窗口设置变更
        local close_main_wnd_oper_info = {close_main_wnd_oper = closemainWndOper};
        comm_center.send_broadcast("i_broadcast_sys_setting_close_main_wnd_oper", close_main_wnd_oper_info);

        local device_mgr =
          base_utility.get_component_adapter("IDevice_mgr",
              "ierd_tgp.device.GetDeviceModule();");
        local password = '';
        local admin_name = '';
        if device_mgr and ("xiaomi" == setting_dialog_model.vendor or "huawei" == setting_dialog_model.vendor) then
            if 1 == iUseRouterDownloadTask then
                device_mgr:SetRouterPredownloadEnabled(true, admin_name, password);
            else
                device_mgr:SetRouterPredownloadEnabled(false, admin_name, password);
            end;
            if 1 == iUseRouterAccelerate then
                device_mgr:SetRouterAcclEnabled(true, admin_name, password);
            else
                device_mgr:SetRouterAcclEnabled(false, admin_name, password);
            end;
        end;
        
        local gamelib =
          base_utility.get_component_adapter("IGame_library",
              "ierd_tgp.game_library.Get_game_library_adapter();");

        local anchor_subscribe_flag = json_param["anchor_subscribe"];
        base_utility.log("iMemoryArrange:anchor_subscribe_flag: ".. anchor_subscribe_flag);
        if anchor_subscribe_flag == 0 then
            gamelib:SetAnchorSubscribeFlag(false);
        else
            gamelib:SetAnchorSubscribeFlag(true);
        end;

        local video_subscribe_flag = json_param["video_subscribe"];
        base_utility.log("iMemoryArrange:video_subscribe_flag: ".. video_subscribe_flag);
        
        setting_dialog_control.set_video_subscribe_flag(video_subscribe_flag);

        if setting_dialog_view.is_dev_client then
            local debug_setting = {};
            debug_setting["skip_anti_addition_check"] = json_param["skip_anti_addition_check"];
            debug_setting["detail_log"] = json_param["detail_log"];
            debug_setting["skip_anti_piracy_check"] = json_param["skip_anti_piracy_check"];
            setting_dialog_control.set_debug_setting(debug_setting);
        end;

        local dpi_setting_flag = json_param["dpi_setting"];
        setting_dialog_control.set_dpi_setting(dpi_setting_flag);

        local data_in = {};
        data_in["auto_start"] = auto_start;
        data_in["auto_login"] = auto_login;
        data_in["download_path"] = download_path;
        data_in["download_speed_limit"] = speed_limit; 
        data_in["download_speed"] = download_speed;
        data_in["setup_path"] = setup_path;
        data_in["close_main_wnd_oper"] = closemainWndOper;
        data_in["auto_download_uncomplete_task"] = iAutoDownloadUncompleteTask;
        data_in["after_install_pkgoper"] = iAfterInstallPkgOper;
        data_in["use_router_download_task"] = iUseRouterDownloadTask;
        data_in["use_router_accelerate"] = iUseRouterAccelerate;
        data_in["start_game_tgp_wnd_oper"] = start_game_tgp_wnd_oper;
        data_in["after_buy_game_oper"] = after_buy_game_oper;
        data_in["open_game_list_cloud_sync"] = open_game_list_cloud_sync;
        data_in["open_wegame_silent_update"] = open_wegame_silent_update;
        data_in["game_screenshot_hotkey"] = game_screenshot_hotkey;
        data_in["game_streaming_enable"] = game_streaming_enable;
        data_in["game_streaming_enable_password"] = game_streaming_enable_password;
        data_in["game_streaming_password"] = game_streaming_password;
        data_in["page_cache_setting"] = page_cache_setting;
        data_in["page_preload_setting"] = page_preload_setting;
        data_in["subscribe_after_open_download"] = subscribe_after_open_download;

        comm_center.call_service("Svr_TGPConfigMgr_SetUserSysSetting", data_in);
--        local param = {
--            cache_switch = page_cache_setting
--        };
--        event_center.send_event(mainframe_event.PAGE_CACHE_SWITCH_CHANGE,
--            base_utility.table_to_json(param));

        -- qos
        for k, v in pairs(setting_dialog_view.qos_track_settings) do
            if data_in[k] ~= nil and v ~= data_in[k] then
                local action = setting_qos_action.kActionChecked;
                if data_in[k] == 0 then
                    action = setting_qos_action.kActionUncheck;
                end
                setting_dialog_control.report_qos_settings(k, action);
            end
        end;
    end;

    on_anchor_subscribe_flag_updated = function(event_id, json_str)
        base_utility.log("on_anchor_subscribe_flag_updated:"..json_str);

        if setting_dialog_view.dialog_ptr == nil then
            base_utility.log("on_anchor_subscribe_flag_updated: pDialog is nil");
            return;
        end
        local result_tbl = base_utility.json_to_table(json_str);
        if result_tbl == nil then
            base_utility.log("on_anchor_subscribe_flag_updated: bad json_str");
            return;
        end
        if result_tbl.flag == nil then
            base_utility.log("on_anchor_subscribe_flag_updated: bad json_str");
            return;
        end
        setting_dialog_view.anchor_subscribe_flag = result_tbl.flag;
        --setting_dialog_view.set_setting_cfg(nil);
        setting_dialog_view.update_live_subscribe_cfg();
    end;

    get_video_subscribe_flag = function()
        comm_center.call_service("i_service_tgp_config_mgr_get_video_subscribe_flag", {});
    end;

    set_video_subscribe_flag = function(flag)
        local param = {};
        param["flag"] = flag;
        comm_center.call_service("i_service_tgp_config_mgr_set_video_subscribe_flag", param);
    end;

    on_subscribe_flag_updated = function(data)
        if data ~= nil and data.flag ~= nil and data.event_name ~= nil then
            base_utility.log("on_video_subscribe_flag_updated:"..base_utility.table_to_json(data));
            if data.event_name == "video_subscribe_flag_update" then
                setting_dialog_view.video_subscribe_flag = data.flag;
            end;
            --setting_dialog_view.set_setting_cfg(nil);
            setting_dialog_view.update_video_subscribe_cfg();
        end;
    end;

    on_hotkey_cfg_updated = function(data)
        if data ~= nil and data.hotkey ~= nil then
            base_utility.log("on_hotkey_cfg_updated:"..base_utility.table_to_json(data));
            setting_dialog_view.update_hotkey_cfg(data.hotkey);
        end;
    end;

    open_setting_dialog_ex = function(data_in)
        if data_in ~= nil and data_in.tab_id ~= nil then
            if type(data_in.tab_id) == "number" then
                if data_in.tab_id >= 0 then
                    local table_para = {tab_id = data_in.tab_id};
                    local data_json = base_utility.table_to_json(table_para);
                    setting_dialog_control.open_setting_dialog(nil, data_json);
                end
            end
        end
    end;

    report_qos_settings = function(name, action)
        local qos_data = ierd_tgp.tpf_ui.LuaQosDataEx:new();
        local kv_data = {};
        kv_data["name" ] = name;
        kv_data["action" ] = action;
        qos_data.oc_type    = 1;
        qos_data.qos_kind   = ierd_tgp.adapt_for_imports.qos.Qos_kind.kGameUpdateLackSpace;
        qos_data.str_action = "client_settings";
        qos_data.str_type   = "download";
        qos_data.str_json   = base_utility.table_to_json(kv_data);
        base_utility.lua_qos_ex(qos_data);
        qos_data:delete();
    end;

    set_dpi_setting = function (dpi_mode)
        local data = {};
        data.mode = dpi_mode;
        comm_center.call_service("Svr_SetDPIAwareMode", data);
    end;

    get_dpi_setting = function ()
        local dpi_mode = 0;
        local dpi_data = comm_center.call_service("Svr_GetDPIAwareMode", {});
        if nil ~= dpi_data then
            dpi_mode = dpi_data.mode;
        end
        return dpi_mode;
    end;

    set_debug_setting = function(debug_setting)
        local data = {};
        data.debug_setting = base_utility.table_to_json(debug_setting);
        comm_center.call_service("Svr_SetDeveloperDebugSetting", data);
    end;

    get_debug_setting = function()
        local debug_setting = {};
        local data = comm_center.call_service("Svr_GetDeveloperDebugSetting", {});
        if data ~= nil then
            debug_setting = base_utility.json_to_table(data.debug_setting);
        end;
        return debug_setting;
    end;

    install_path_mgr_info_update = function(data)
        base_utility.log("setting_dialog_control.install_path_mgr_info_update");
        if data ~= nil and data.mgr_info ~= nil and data.mgr_info ~= "" then
            local info = base_utility.json_to_table(data.mgr_info);
            if setting_dialog_view ~= nil then
                setting_dialog_view.update_install_path_setting_ui(info.data);
            end
        end
    end;

    on_main_wnd_show = function(data)
        if data ~= nil and data.isfirst == 1 then
            setting_dialog_control.set_subscribe_after_open_dl_val(
                setting_dialog_control.get_subscribe_after_open_dl_local());
            setting_dialog_control.get_subscribe_after_open_dl_cfg();
        end;
    end;

    get_subscribe_after_open_dl_cfg = function()
        setting_dialog_control.inner_log("get_subscribe_after_open_dl_cfg");
        local data = {};
        data["config_list"] = {};
        data["result"] = 0;
        local item = {};
        item.key = setting_dialog_model.subscribe_after_open_download_server_key;
        table.insert(data["config_list"], item);
        comm_center.call_service_asyn("i_service_get_cloud_storage_list", data, 
            "setting_dialog_control.on_get_subscribe_after_open_dl_cfg");
    end;

    on_get_subscribe_after_open_dl_cfg = function(data)
        setting_dialog_control.inner_log(
          "on_get_subscribe_after_open_dl_cfg : "..base_utility.table_to_json(data));
        if data.result ~= nil and data.result == 0 and data.config_list ~= nil then
           setting_dialog_control.update_subscribe_after_open_dl_cfg(data);
        end;
    end;

    set_subscribe_after_open_dl_cfg = function(switch)
        setting_dialog_control.inner_log("set_subscribe_after_open_dl_cfg : "..tostring(switch));
        setting_dialog_control.set_subscribe_after_open_dl_val(switch);
        local data = {};
        data["config_list"] = {};
        data["result"] = 0;
        local item = {};
        item.key = setting_dialog_model.subscribe_after_open_download_server_key;
        item.value = tostring(switch);
        table.insert(data["config_list"], item);
        comm_center.call_service_asyn("i_service_set_cloud_storage_list", data, 
            "setting_dialog_control.on_set_subscribe_after_open_dl_cfg");
    end;

    on_set_subscribe_after_open_dl_cfg = function(data)
        setting_dialog_control.inner_log(
          "on_set_subscribe_after_open_dl_cfg : "..base_utility.table_to_json(data));
    end;

    get_subscribe_after_open_dl_local = function()
        local ret = 0;
        local data = {};
        data["common"] = 1;
        data["key"] = setting_dialog_model.subscribe_after_open_dl_cfg_key;
        local config = comm_center.call_service("Svr_GetConfig", data);
        if nil ~= config and 1 == config.exits then
            ret = tonumber(config.value or "0");
        else
            setting_dialog_control.inner_log("subscribe_after_open_dl local cfg not exist");
        end
        setting_dialog_control.inner_log("get_subscribe_after_open_dl_local:"..tostring(ret));
        return ret;
    end;

    set_subscribe_after_open_dl_local = function(val)
        local data = {};
        data["common"] = 1;
        data["key"] = setting_dialog_model.subscribe_after_open_dl_cfg_key;
        data["value"] = tostring(val);
        comm_center.call_service("Svr_SetConfig", data);
        setting_dialog_control.inner_log("set_subscribe_after_open_dl_local:"..tostring(val));
    end;

    update_subscribe_after_open_dl_cfg = function(data)
        if data == nil or data.config_list == nil or type(data.config_list) ~= "table" or
           next(data.config_list) == nil then
            return;
        end;

        for index, item in pairs(data.config_list) do
            if item ~= nil and item.key ~= nil and item.value ~= nil and item.exits ~= nil and 
               item.exits == 1 and 
               item.key == setting_dialog_model.subscribe_after_open_download_server_key then 
                local server_val = tonumber(item.value);
                if server_val == 0 or server_val == 1 then
                    setting_dialog_control.set_subscribe_after_open_dl_val(server_val);
                    if setting_dialog_view ~= nil then
                        setting_dialog_view.refresh_subscribe_after_open_dl_ui();
                    end;
                    setting_dialog_control.set_subscribe_after_open_dl_local(
                        setting_dialog_model.subscribe_after_open_download);
                else
                    setting_dialog_control.inner_log("subscribe_download_switch value invalid");
                end;
            end;
        end;
    end;

    on_cloud_storage_update = function(data)
        if data == nil or type(data) ~= "table" then
            return;
        end;
        setting_dialog_control.inner_log(
          "on_cloud_storage_update : "..base_utility.table_to_json(data));
        setting_dialog_control.update_subscribe_after_open_dl_cfg(data);
    end;
    
    set_subscribe_after_open_dl_val = function(value)
        setting_dialog_control.inner_log("set_subscribe_after_open_dl_val:"..tostring(value));
        setting_dialog_model.subscribe_after_open_download = value;
    end;

    operate_game_duration = function(gameid, duration_type, op_type)
        local data = {};
        local params = {};
        params.game_id = gameid;
        params.duration_type = duration_type;
        params.op_type = op_type;
        data.params = base_utility.table_to_json(params);
        comm_center.call_service("Svr_OperateGameDuration", data);
    end;

    on_operation_duration_rsp = function(data)
        setting_dialog_view.on_operation_duration_rsp(data);
    end;
};

setting_dialog_control.init();
